﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using Web.UI;
using Model;
using System.Collections;

namespace Web.admin.BasicManage.Classes
{
    public partial class AddClasses : System.Web.UI.Page
    {
        ClassService classService = new ClassService();
        MajorService majorService = new MajorService();
        LocalService localService = new LocalService();
        AcademyService academyService = new AcademyService();
        protected static BoClasses classorg;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MajorBind("未确定");
                LocalBind("未确定");
                AcademyBind("未确定");
                string operation = Request["OPERATION"];
                string classesId = Request["CLA_ID"];
                if (operation == "Modify")
                {
                    if (classesId == null)
                    {
                        Response.Write("<script>alert('不允许修改地址');window.location.href='CLA_List.aspx'</script>");
                    }
                    ModifyClasses(classesId);
                }
                else if (operation == "Delete")
                {
                    if (classesId == null)
                    {
                        Response.Write("<script>alert('不允许修改地址');window.location.href='CLA_List.aspx'</script>");
                    }
                    DeleteClasses(classesId);
                }
            }

        }

        /// <summary>
        /// 专业绑定
        /// </summary>
        protected void MajorBind(string fistText)
        {
            MajorList.Items.Clear();
            if (!string.IsNullOrEmpty(fistText))
            {
                MajorList.Items.Add(new ListItem(fistText, ""));
            }
            IList<BoMajor> majorList = majorService.GetAllMajors();
            foreach (BoMajor model in majorList)
            {
                MajorList.Items.Add(new ListItem(model.MAJ_NAME, model.MAJ_ID));
            }
        }


        /// <summary>
        /// 校区绑定
        /// </summary>
        protected void LocalBind(string fistText)
        {
            localList.Items.Clear();
            if (!string.IsNullOrEmpty(fistText))
            {
                localList.Items.Add(new ListItem(fistText, ""));
            }
            IList<BoLocal> LocalList = localService.getAllLocal();
            foreach (BoLocal model in LocalList)
            {
                localList.Items.Add(new ListItem(model.LOC_NAME, model.LOC_ID));
            }
        }
        /// <summary>
        /// 学院绑定
        /// </summary>
        protected void AcademyBind(string fistText)
        {
            ACA_List.Items.Clear();
            if (!string.IsNullOrEmpty(fistText))
            {
                ACA_List.Items.Add(new ListItem(fistText, ""));
            }
            IList<BoAcademy> ACAList = academyService.getAllAcademies();
            foreach (BoAcademy model in ACAList)
            {
                ACA_List.Items.Add(new ListItem(model.ACA_NAME, model.ACA_ID));
            }
        }

        /// <summary>
        /// 根据班级id来修改班级
        /// </summary>
        /// <param name="classesId">班级id</param>
        protected void ModifyClasses(string classesId)
        {
            BoClasses classes = classService.getClass(classesId);
            TextBoxName.Text = classes.CLA_NAME;
            TextBoxCode.Text = classes.CLA_CODE;
            TextBoxShort.Text = classes.CLA_SHORT;
            TextBoxEnName.Text = classes.CLA_ENNAME;
            TextBoxSYSTYPE.Text = classes.CLA_SYSTYPE;
            TextBoxSTUNUM.Text = classes.CLA_STUNUM.ToString();
            TextBoxGRADE.Text = classes.CLA_GRADE;
            TextBoxINDATE.Text = classes.CLA_INDATE.ToString();
            TextBoxRemark.Text = classes.CLA_REMARK;
            MajorList.SelectedValue = classes.CLA_MAJOR;
            localList.SelectedValue = classes.CLA_LOCAL;
            ACA_List.SelectedValue = classes.CLA_ACADEMY;
            CLAIDBOX.Value = classesId;
            classorg = classes;
        }

        /// <summary>
        /// 根据班级id来删除班级
        /// </summary>
        /// <param name="classesId">班级id</param>
        protected void DeleteClasses(string classesId)
        {
            BoClasses classes = new BoClasses();
            classes.CLA_ID = classesId;
            classService.DeleteClass(classesId);
            Response.Redirect("CLA_List.aspx");
        }

        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            //DateTime time = new DateTime();
            //BoClasses classes = new BoClasses();
            //classes.CLA_NAME = TextBoxName.Text;
            //classes.CLA_CODE = TextBoxCode.Text;
            //classes.CLA_SHORT = TextBoxShort.Text;
            //classes.CLA_ENNAME = TextBoxEnName.Text;
            //classes.CLA_SYSTYPE = TextBoxSYSTYPE.Text;
            //classes.CLA_STUNUM = Int32.Parse(TextBoxSTUNUM.Text);
            //classes.CLA_GRADE = TextBoxGRADE.Text;
            //if (DateTime.TryParse(TextBoxINDATE.Text, out time))
            //{ 
            //    classes.CLA_INDATE  = time;
            //}
            //classes.CLA_REMARK = TextBoxRemark.Text;
            //classes.CLA_MAJOR = MajorList.SelectedValue;
            //classes.CLA_LOCAL = localList.SelectedValue;
            //classes.CLA_ACADEMY = ACA_List.SelectedValue;
            //if (string.IsNullOrEmpty(CLAIDBOX.Value))
            //{
            //    classService.CreateClass(classes);
            //    Response.Redirect("CLA_List.aspx");
            //}
            //else
            //{
            //    classes.CLA_ID = CLAIDBOX.Value;
            //    classService.UpdateClass(classes);
            //    Response.Redirect("CLA_List.aspx");
            //}
            BoClasses classes = new BoClasses();
            classes.CLA_NAME = TextBoxName.Text;
            classes.CLA_CODE = TextBoxCode.Text;
            classes.CLA_SHORT = TextBoxShort.Text;
            classes.CLA_ENNAME = TextBoxEnName.Text;
            classes.CLA_SYSTYPE = TextBoxSYSTYPE.Text;
            try
            {
                classes.CLA_STUNUM = Int32.Parse(TextBoxSTUNUM.Text);
            }
            catch 
            { }
            classes.CLA_GRADE = TextBoxGRADE.Text;
            try
            {
                classes.CLA_INDATE = Convert.ToDateTime(TextBoxINDATE.Text);
            }
            catch { }
            classes.CLA_REMARK = TextBoxRemark.Text;
            classes.CLA_MAJOR = MajorList.SelectedValue;
            classes.CLA_LOCAL = localList.SelectedValue;
            classes.CLA_ACADEMY = ACA_List.SelectedValue;

            BoCheckRepat repeat = new BoCheckRepat();
            repeat.tablename = "Classes";
            repeat.constr = "CLA_IsShow";

            Hashtable keyValue = new Hashtable();
            keyValue.Add("CLA_LOCAL", classes.CLA_LOCAL);
            keyValue.Add("CLA_ACADEMY", classes.CLA_ACADEMY);
            keyValue.Add("CLA_MAJOR", classes.CLA_MAJOR);

            repeat.content = keyValue;
            if (string.IsNullOrEmpty(CLAIDBOX.Value))
            {
                if (checkrepeat.checkrepeatCount(repeat) > 0)
                {
                    Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                }
                else
                {
                    classService.CreateClass(classes);
                    Response.Redirect("CLA_List.aspx");
                }
            }
            else
            {
                        classes.CLA_ID = CLAIDBOX.Value;
                        classService.UpdateClass(classes);
                        Response.Redirect("CLA_List.aspx");         
            }
        }

    }
}